// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import PlateNameItem from "../../views/items/PlateNameItem";

const {ccclass, property} = cc._decorator;

@ccclass
export default class PlateListComponent extends cc.Component {

    @property(cc.Label)
    titleLabel: cc.Label = null;

    @property(cc.Prefab)
    plateItemPrefab: cc.Prefab = null;

    @property(cc.Node)
    content: cc.Node = null;

    _itemArray: any = [];
    
    // onLoad () {}

    clearData () {
        for (let index = 0; index < this._itemArray.length; index++) {
            const node = this._itemArray[index];
            node.getComponent(PlateNameItem).clearItem();
        }
    }

    setData (list, title, isToggle) {
        this.titleLabel.string = title;
        this.clearData();

        for (let index = 0; index < list.length; index++) {
            const data = list[index];
            let node = this._itemArray[index];
            if (node == null) {
                node = cc.instantiate(this.plateItemPrefab);
                this.content.addChild(node);
                this._itemArray[index] = node;
            }
            node.active = true;
            let item = node.getComponent(PlateNameItem);
            item.setItem(data, (ts_code, isToggle)=>{
                this.onItemClick(ts_code, isToggle);
            }, isToggle);
        }
    }

    onItemClick (itemInfo, isToggle) {
        if (isToggle == false) {
            cc.director.emit('SelectPlateEvent', itemInfo);
        }
    }

    setMatchText (text) {
        for (let index = 0; index < this._itemArray.length; index++) {
            const node = this._itemArray[index];
            let plate = node.getComponent(PlateNameItem);
            if (plate.isUsed()) {
                plate.matchText(text);
            }
        }
    }

    setSelect (plates) {
        for (let index = 0; index < plates.length; index++) {
            const ts_code = plates[index];
            for (let index = 0; index < this._itemArray.length; index++) {
                const node = this._itemArray[index];
                let plate = node.getComponent(PlateNameItem);
                if (plate.isUsed() && ts_code === plate.ts_code) {
                    plate.setSelect(true);
                }
            }
        }
    }

    getSelect () {
        let selects = [];
        for (let index = 0; index < this._itemArray.length; index++) {
            const node = this._itemArray[index];
            let plate = node.getComponent(PlateNameItem);
            if (node.active && plate.isSelect()) {
                selects.push(plate.ts_code);
            }
        }
        return selects;
    }

    // update (dt) {}
}
